package com.ccjt.efcfs.repaymentplan;

import com.alibaba.fastjson.JSON;
import com.ccjt.efcfs.dto.LoanInfo;
import com.ccjt.efcfs.enums.RepayMothodTypeEnum;
import static com.ccjt.efcfs.repaymentplan.AverageCapitalUtils.getInterestCount;
import static com.ccjt.efcfs.repaymentplan.AverageCapitalUtils.getPerMonthInterest;
import static com.ccjt.efcfs.repaymentplan.AverageCapitalUtils.getPerMonthPrincipal;
import static com.ccjt.efcfs.repaymentplan.AverageCapitalUtils.getPerMonthPrincipalInterest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

/**
 * java 等额本金等额本息工具类 https://my.oschina.net/ydsakyclguozi/blog/538210
 *
 * 海南省住房公积金管理局-贷款计算器 http://gjj.hainan.gov.cn/hngjj/dkjsq/list_tt.shtml
 *
 *
 * 个人住房公积金贷款计算 房屋总价：600000元 贷款年数：25年 首付：2成 年利率：3.25%
 *
 * 计算结果 房款总额：600000元 贷款总额：480000元 首期付款：120000元 期数：300期
 *
 * 计算方式：等额本金 支付息款：195650元 还款总额：675650元 首月还款：2900元
 *
 * 计算方式：等额本息 支付息款：221735.36元 还款总额：701735.36元 首月还款：2339.12元
 *
 * 计算结果仅供参考，以最终受理结果为准。
 *
 */
public class GenerationPlan {

    public final static double invest = 480000; // 本金
    public final static int month = 300; // 期数
    public final static double yearRate = 0.0325; // 年利率

    /**
     * 等额本金 equality corpus
     */
    private static void averageCapital() {
        Map<Integer, Double> getPerMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
        System.out.println("等额本金---每月本息：" + getPerMonthPrincipalInterest);
        double benjin = getPerMonthPrincipal(invest, month);
        System.out.println("等额本金---每月本金:" + benjin);
        Map<Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, month);
        System.out.println("等额本金---每月利息:" + mapInterest);
        double count = getInterestCount(invest, yearRate, month);
        System.out.println("等额本金---总利息：" + count);
    }

    /**
     * 等额本息 equality corpus and interest
     */
    private static void averageCapitalPlusInterest() {
        double perMonthPrincipalInterest = AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(invest, yearRate, month);
        System.out.println("等额本息---每月还款本息：" + perMonthPrincipalInterest);
        Map<Integer, BigDecimal> mapInterest = AverageCapitalPlusInterestUtils.getPerMonthInterest(invest, yearRate, month);
        System.out.println("等额本息---每月还款利息：" + mapInterest);
        Map<Integer, BigDecimal> mapPrincipal = AverageCapitalPlusInterestUtils.getPerMonthPrincipal(invest, yearRate, month);
        System.out.println("等额本息---每月还款本金：" + mapPrincipal);
        double count = AverageCapitalPlusInterestUtils.getInterestCount(invest, yearRate, month);
        System.out.println("等额本息---总利息：" + count);
        double principalInterestCount = AverageCapitalPlusInterestUtils.getPrincipalInterestCount(invest, yearRate, month);
        System.out.println("等额本息---应还本息总和：" + principalInterestCount);
    }

    public static void main(String[] args) {
//        averageCapital();

        System.out.println();
        System.out.println();
        System.out.println();

//        averageCapitalPlusInterest();
        LoanInfo loan = null;
//        LoanInfo loan = new LoanInfo(0.0325, 480000.0, 300);
        loan = new LoanInfo(0.0325, 480000.0, 300, "2019-11-01", "2019-12-01", "2020-11-30");

        BigDecimal monRate = loan.getYearRate().divide(new BigDecimal(12), 6, RoundingMode.HALF_UP); // 月利率
//        BigDecimal monPrincipal = li.getPrincipal().divide(li.getPeriods()); // 分期本金
        BigDecimal monPrincipal = loan.getLoanPrincipal().divide(loan.getPlanPeriods(), 6, RoundingMode.HALF_UP); // 分期本金
        System.out.println(monRate.doubleValue());
        System.out.println(monPrincipal.doubleValue());

        // 等额本金
        System.out.println(JSON.toJSONString(AverageCapitalCalUtils.getInstance().generationPlan(loan, RepayMothodTypeEnum.DEBJ)));

        System.out.println();
        System.out.println();
        System.out.println();

        // 等额本息
        System.out.println(JSON.toJSONString(AverageCapitalCalUtils.getInstance().generationPlan(loan, RepayMothodTypeEnum.DEBX)));

        System.out.println();
        System.out.println();
        System.out.println();

        // 等本等息
        System.out.println(JSON.toJSONString(AverageCapitalCalUtils.getInstance().generationPlan(loan, RepayMothodTypeEnum.DBDX)));

    }

}
